package com.kim.smartring.activity.fragment;

import java.util.Calendar;

import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.kim.smartring.R;
import com.kim.smartring.activity.SmartRingMainActivity;
import com.kim.smartring.ble.BleNamesResolver;
import com.kim.smartring.broadcastreceiver.BLEBroadcastReceiver;
import com.kim.smartring.utils.BLEDataUtils;
import com.kim.smartring.utils.ConfigUtils;
import com.kim.smartring.utils.DateUtils;
import com.kim.smartring.utils.DialogUtils;

public class AlarmSettingsFragment extends BaseFragment {

	private TextView txtAlarmTime;
	private Button modifyAlarmTime;
	private Spinner mWeekdays;

	private int weekday = 1;

	private Calendar calendar = Calendar.getInstance();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View contentView = inflater.inflate(R.layout.alarm_settings, null);

		txtAlarmTime = (TextView) contentView.findViewById(R.id.txt_alarm_time);
		modifyAlarmTime = (Button) contentView
				.findViewById(R.id.btn_modify_alarm_time);
		mWeekdays = (Spinner) contentView.findViewById(R.id.sp_weekday);
		mWeekdays.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				weekday = position;
				ConfigUtils.setWeekdayIndex(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		modifyAlarmTime.setOnClickListener(this);

		return contentView;
	}

	@Override
	public void onResume() {
		super.onResume();
		showAlarmTime();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_modify_alarm_time:
			showTimePicker();
			break;
		}
	}

	private void showAlarmTime() {
		long alarmTime = ConfigUtils.getAlarmTime();
		if (0 == alarmTime) {
			txtAlarmTime.setText("");
			modifyAlarmTime.setText("设置闹钟时间");
		} else {
			calendar.setTimeInMillis(alarmTime);
			String timeStr = DateUtils.getHourSecond(alarmTime);
			txtAlarmTime.setText(timeStr);
			modifyAlarmTime.setText("修改闹钟时间");
		}
		int weekdayIndex = ConfigUtils.getWeekdayIndex();
		mWeekdays.setSelection(weekdayIndex);
	}

	private void showTimePicker() {
		DialogUtils.showTimeDialog(calendar, getActivity(),
				new OnDismissListener() {

					@Override
					public void onDismiss(DialogInterface dialog) {
						ConfigUtils.setAlarmTime(calendar.getTimeInMillis());
						writeAlarmSetting();
						showAlarmTime();
					}
				});
	}

	private void writeAlarmSetting() {

		SmartRingMainActivity mainActivity = null;
		try {
			mainActivity = (SmartRingMainActivity) getActivity();
			String data = BLEDataUtils.buildAlarmData(
					calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE),
					weekday + 1);

			mainActivity.writeDataToSmartRing(data,
					BleNamesResolver.UUID_USERINFO,
					BleNamesResolver.UUID_C_USERINFO);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}
}
